/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.entitygen.enrichedassoudt;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.api.attr.AssoUdtAtrribute;
import com.inspur.edp.cef.gencommon.ReflectionUtils;
import com.inspur.edp.cef.generator.common.MediatePropertyInfo;
import com.inspur.edp.das.commonmodel.IGspCommonElement;
import com.inspur.edp.udt.designtime.api.entity.SimpleDataTypeDef;
import java.lang.reflect.Method;
import java.util.ArrayList;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;

//    
//     public BEUserUdt(IUserUdt userUdt) => 
//            Info = new BEUserInfo(userUdt?.Info);
//     
public class EnrichedAssoUdtCtorGenerator extends ClassMethodGenerator {

  private static final String parName = "udt";
  private SimpleDataTypeDef udtType;
  private MediatePropertyInfo enrichedType;
  private String className;

  public EnrichedAssoUdtCtorGenerator(SimpleDataTypeDef udtType, MediatePropertyInfo enrichedType,
      String className) {
    super();
    this.udtType = udtType;
    this.enrichedType = enrichedType;
    this.className = className;
  }

  @Override
  protected boolean buildIsConstructor() {
    return true;
  }

  @Override
  protected String getMethodName() {
    return className;
  }

  @Override
  protected TypeInfo getReturnType() {
    return null;
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
    ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
    list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
    return list;
  }

  @Override
  protected ArrayList<ParameterInfo> getParameterCollection() {
    ArrayList<ParameterInfo> list = new ArrayList<>();
    ParameterInfo info = new ParameterInfo();
    info.setParamType(new TypeInfo(udtType.getGeneratedEntityClassInfo().getClassFullName()) {{
      setUseFullName(true);
    }});
    info.setParamName(parName);
    list.add(info);
    return list;
  }

  @Override
  protected Block buildMethodBody() {
    Block block = ast.newBlock();
    IfStatement ifStatement = getIfStatement();
    block.statements().add(ifStatement);
    return block;
  }

  private IfStatement getIfStatement() {
    IfStatement ifStatement = ast.newIfStatement();
    setIfExpression(ifStatement);
    setIfThenStatemnt(ifStatement);
    setIfElseStatemnt(ifStatement);
    return ifStatement;
  }

  private void setIfElseStatemnt(IfStatement ifStatement) {
    MethodInvocation methodInvocation = ast.newMethodInvocation();
    methodInvocation.setName(ast.newSimpleName("set" + enrichedType.getElement().getLabelID()));
    ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
    classInstanceCreation.setType(ast.newSimpleType(ast.newName(getAssociationTypeName())));
    classInstanceCreation.arguments().add(getElseClassCreateParam());
    methodInvocation.arguments().add(classInstanceCreation);
    ExpressionStatement expressionStatement = ast.newExpressionStatement(methodInvocation);
    ifStatement.setElseStatement(expressionStatement);
  }

  private MethodInvocation getElseClassCreateParam() {
    MethodInvocation methodInvocation = ast.newMethodInvocation();
    methodInvocation.setName(ast.newSimpleName("get" + udtType.getContainElements().get(0).getLabelID()));
    methodInvocation.setExpression(ast.newSimpleName(parName));
    return methodInvocation;
  }

  private Method getInfoMethod(Class c) {
    throw new UnsupportedOperationException();
  }

  private String getSetPropertyClassName(Class type) {
    for (Method method : type.getMethods()) {
      AssoUdtAtrribute assoUdtAtrribute = (AssoUdtAtrribute) ReflectionUtils
          .getMethodAnnotation(method, AssoUdtAtrribute.class);
      if (assoUdtAtrribute == null) {
        continue;
      }
      return String.format("set%1$s", assoUdtAtrribute.parentPropName());
    }
    throw new RuntimeException();
  }

  private String getGetPropetyMethodName(Class type) {
    for (Method method : type.getMethods()) {
      AssoUdtAtrribute assoUdtAtrribute = (AssoUdtAtrribute) ReflectionUtils
          .getMethodAnnotation(method, AssoUdtAtrribute.class);
      if (assoUdtAtrribute == null) {
        continue;
      }
      return String.format("get%1$s", assoUdtAtrribute.parentPropName());
    }
    throw new RuntimeException();
  }

  private Class getGetPropertyClassType(Class type) {
    for (Method method : type.getMethods()) {
      AssoUdtAtrribute assoUdtAtrribute = (AssoUdtAtrribute) ReflectionUtils
          .getMethodAnnotation(method, AssoUdtAtrribute.class);
      if (assoUdtAtrribute == null) {
        continue;
      }
      return method.getReturnType();
    }
    throw new RuntimeException();
  }

  private void setIfThenStatemnt(IfStatement ifStatement) {
    MethodInvocation methodInvocation = ast.newMethodInvocation();
    methodInvocation.setName(ast.newSimpleName("set" + enrichedType.getElement().getLabelID()));
    ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
    classInstanceCreation.setType(ast.newSimpleType(ast.newName(getAssociationTypeName())));
    classInstanceCreation.arguments().add(ast.newNullLiteral());
    methodInvocation.arguments().add(classInstanceCreation);
    ExpressionStatement expressionStatement = ast.newExpressionStatement(methodInvocation);
    ifStatement.setThenStatement(expressionStatement);
  }

  private void setIfExpression(IfStatement ifStatement) {
    InfixExpression expression = ast.newInfixExpression();
    expression.setOperator(InfixExpression.Operator.EQUALS);
    expression.setLeftOperand(ast.newSimpleName(parName));
    expression.setRightOperand(ast.newNullLiteral());
    ifStatement.setExpression(expression);
  }

  public String getAssociationTypeName() {
    return enrichedType.getElement().getBelongObject().getGeneratedEntityClassInfo()
        .getClassNamespace() + "." + ((IGspCommonElement) enrichedType.getElement())
        .getAssociationTypeName();
  }
}
